import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../../services/screen_adapter.dart';
import './cart_num.dart';
import '../../provider/cart.dart';

class CartItem extends StatefulWidget {

  final Map _itemData;

  CartItem(this._itemData, {Key key}) : super(key: key);

  @override
  _CartItemState createState() => _CartItemState();
}

class _CartItemState extends State<CartItem> {

  Map _itemData;

  @override
  Widget build(BuildContext context) {
    
    ScreenAdapter.init();
    /// 注意：initSetData里只是初始化的时候赋值，重新build时不再赋值了
    _itemData = widget._itemData;

    return Container(
      padding: EdgeInsets.all(5),
      height: ScreenAdapter.height(220),
      decoration: BoxDecoration(
        border: Border(
          bottom: BorderSide(
            width: 1,
            color: Colors.black12
          )
        )
      ),
      child: Row(
        children: <Widget>[
          Container(
            width: ScreenAdapter.width(60),
            child: Checkbox(
              value: _itemData['checked'], 
              onChanged: (value) {
                _itemData['checked'] = !_itemData['checked'];
                context.read<Cart>().itemChange();
              },
              activeColor: Colors.pink,
            )
          ),
          Container(
            width: ScreenAdapter.width(160),
            child: Image.network('${_itemData['pic']}', fit:BoxFit.cover)
          ),
          Expanded(
            flex: 1,
            child: Container(
              padding: EdgeInsets.fromLTRB(10, 10, 5, 10),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween, // 一个居上一个居下
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text('${_itemData['title']}', maxLines: 2),
                  Text('${_itemData['selectedAttr']}', maxLines: 2),
                  Stack(
                    children: <Widget>[
                      Align(
                        alignment: Alignment.centerLeft,
                        child: Text('¥${_itemData['price']}', style: TextStyle(
                          color: Colors.red
                        ))
                      ),
                      Align(
                        alignment: Alignment.centerRight,
                        child: CartNum(_itemData)
                      )
                    ]
                  )
                ]
              )
            )
          )
        ]
      )
    );
  }
}